
--
-- Author: gnar
-- Date: 2015-12-31 23:23:22
--
ccb.MainSceneCCB = {}

local Camera = import("..entityModels.Camera")
local Types = import("..entityModels.Types")
local MainSceneCCB = class("MainSceneCCB", cc.load("mvc").ViewBase)
local scheduler = cc.Director:getInstance():getScheduler()
local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
local ChatLayerCCB = import("..viewsComponent.ChatLayerCCB")


MainSceneCCB.MOVE_TOUCH_TIME = 0.4
MainSceneCCB.WHEEL_2_MAP = 1 --wheel移动一单位映射到地图上移动的距离系数（wheel原点相当于player位置）
MainSceneCCB.WHEEL_RESPONSE_DIS = 50 --距离wheel原点多远才开始响应
MainSceneCCB.PLAYER_SPEED = 0.5 --这个数值相当于player走100像素距离需要的时间

function MainSceneCCB:ctor()
    MainSceneCCB.super.ctor(self)
end

function MainSceneCCB:onCreate()

    self.luafilePath = "views.MainSceneCCB"
    self:getCcbNode():addTo(self)

    ChatLayerCCB.initWithTarget(self)

    _createMap(self)
end

function MainSceneCCB:onEnter_()
	ManagerNotification:registerObserver("chat.newChat",self,ChatLayerCCB.onNewChat) 
end

function MainSceneCCB:onExit_()
	MainSceneCCB.super.onExit_()
	ManagerNotification:unregisterObserver("chat.newChat",self)
end

_createMap = function(self)
	-- register listener
    local listener = cc.EventListenerTouchOneByOne:create()
    listener:setSwallowTouches(true)
    listener:registerScriptHandler(handler(self, self.onTouchBegan), cc.Handler.EVENT_TOUCH_BEGAN)
    listener:registerScriptHandler(handler(self, self.onTouchMoved), cc.Handler.EVENT_TOUCH_MOVED)
    listener:registerScriptHandler(handler(self, self.onTouchEnded), cc.Handler.EVENT_TOUCH_ENDED)
    --eventDispatcher:addEventListenerWithFixedPriority(listener,Constants.mapNodeLisPriority)
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self.mapNode)
	local map = ManagerMap.getMapNode("1_1"):addTo(self.mapNode)
	self.camera_ = Camera.new(map)
	ManagerMap:init(map, self.camera_)
	
	local player = require("app.entityModels.Player").new()
	player:getView():addTo(map)
	player:setMapPos({x = 60, y = 50})
	self.camera_:look(player, 0)
	player:on("move", function() self:playerMoveHandler(player) end)
	ManagerMap:setPlayer(player)

	-- self:showWheel()
	-- self:hideWheel()
end

function MainSceneCCB:onTouchBegan(touch, event)
	if self.wheelTouchCallSche then
		scheduler:unscheduleScriptEntry(self.wheelTouchCallSche)
		self.wheelTouchCallSche = nil
	end
	if self.isChatShow then
     	ChatLayerCCB["mAnimationManager"]:runAnimationsForSequenceNamedTweenDuration("hideChat", 0)
 	end
	return touchBegan(touch, event, self.mapNode)  
end

function MainSceneCCB:onTouchMoved(touch, event)
	if self.moveTouchCallSche == nil  then
		local mapPospx = ManagerMap.map:convertToNodeSpace(touch:getLocation())
		ManagerMap:walkToPos(mapPospx)
		self.moveTouchCallSche = scheduler:scheduleScriptFunc(function()
			local mapPospx = ManagerMap.map:convertToNodeSpace(touch:getLocation())
			ManagerMap:walkToPos(mapPospx)
		end, MainSceneCCB.MOVE_TOUCH_TIME, false)
	end

end

function MainSceneCCB:onTouchEnded(touch, event)

	if not self.moveTouchCallSche then
		local mapPospx = ManagerMap.map:convertToNodeSpace(touch:getLocation())
		ManagerMap:walkToPos(mapPospx)
	else
		scheduler:unscheduleScriptEntry(self.moveTouchCallSche)
		self.moveTouchCallSche = nil
	end
end

function MainSceneCCB:playerMoveHandler(user)
	--print("test 121qqq",user)
	--self.camera_:look(user, 1)
	-- local view = user:getView()
	-- local pos = view:convertToWorldSpace(cc.p(0, 0))
	-- if pos.x < display.width/3 or pos.x > display.width/3*2
	-- 	or pos.y < display.height/3 or pos.y > display.height/3*2 then
	-- 	print("test 213213qqqaa")
	-- 	self.camera_:look(user, 1)
	-- end
end







-- function MainSceneCCB:hideWheel()

-- 	self.wheel:setVisible(false)
-- 	if self.wheel.listener then
-- 		eventDispatcher:removeEventListener(self.wheel.listener)
-- 	end

-- end

-- function MainSceneCCB:showWheel()
	
-- 	self.wheel:setVisible(true)
-- 	self:addTouchToWheel()

-- end

-- function MainSceneCCB:addTouchToWheel()
    
--     self.wheel.walk = function(touch)
-- 		local touchPos = touch:getLocation()
--     	local relativeToPos = cc.p(touchPos.x - self.wheel:getPositionX(),touchPos.y - self.wheel:getPositionY())

--     	local dis = math.sqrt((math.pow(relativeToPos.x,2) + math.pow(relativeToPos.y,2)))
--     	if dis > MainSceneCCB.WHEEL_RESPONSE_DIS then
--     		local w = function()
--     			local view = ManagerMap.player:getView()
--     			local toPos = cc.p(view:getPositionX() + relativeToPos.x * MainSceneCCB.WHEEL_2_MAP,
-- 		    		view:getPositionY() + relativeToPos.y * MainSceneCCB.WHEEL_2_MAP)
-- 				ManagerMap:walkToPos(toPos)
--     		end
--     		w()
--     		local timeOneTime = dis *  MainSceneCCB.WHEEL_2_MAP / 100 * MainSceneCCB.PLAYER_SPEED 
--     		self.wheelTouchCallSche = scheduler:scheduleScriptFunc(function()
--     			w()
-- 			end, timeOneTime, false)
-- 		end
-- 	end

-- 	local function onTouchBegan(touch, event)
--         local target = event:getCurrentTarget()
--         if self.wheelTouchCallSche then
-- 			scheduler:unscheduleScriptEntry(self.wheelTouchCallSche)
-- 			self.wheelTouchCallSche = nil
-- 		end
--         local locationInNode = target:convertToNodeSpace(touch:getLocation())
--         local s = target:getContentSize()
--         local rect = cc.rect(0, 0, s.width, s.height)
        
--         if cc.rectContainsPoint(rect, locationInNode) then
--             return true
--         end
--         return false
--     end

--     local function onTouchMoved(touch, event)
--     end

--     local function onTouchEnded(touch, event)
--     	self.wheel.walk(touch)
--     end

--     self.wheel.listener = cc.EventListenerTouchOneByOne:create()
--     self.wheel.listener:setSwallowTouches(true)
--     self.wheel.listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
--     self.wheel.listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
--     self.wheel.listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
--     local eventDispatcher = self:getEventDispatcher()
--     eventDispatcher:addEventListenerWithSceneGraphPriority(self.wheel.listener, self.wheel)

-- end



return MainSceneCCB